﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Entity.Table;
using SqlHelper;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class CheckAccountDetailTempDatabase
    {
        /// <summary>
        /// 添加一条明细对帐文件信息
        /// </summary>
        /// <param name="detail">实体对象</param>
        /// <returns></returns>
        public static bool Insert(TB_CheckAccountDetailTemp detail)
        {
            bool ret = true;
            SqlConnection conn = null;

            SqlParameter[] paramList = new SqlParameter[] { 
                                                            DbHelper.CreateCommandParameter("@BankCode",SqlDbType.VarChar,detail.BankCode),
                                                            DbHelper.CreateCommandParameter("@BusinessDate",SqlDbType.VarChar,detail.BusinessDate),
                                                            DbHelper.CreateCommandParameter("@BankWaterNumber",SqlDbType.VarChar,detail.BankWaterID),
                                                            DbHelper.CreateCommandParameter("@UserID",SqlDbType.VarChar,detail.GasUserID),
                                                            DbHelper.CreateCommandParameter("@PayMoney",SqlDbType.VarChar,detail.PaymentMoney.Trim()),
                                                             DbHelper.CreateCommandOutParameter("@return",SqlDbType.Int)
                                                           };
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                ret = DbHelper.ExecuteNonQueryByProc("NXS_CheckAccountDetailTemp_Insert", paramList, conn) > 0;
            }
            catch (SqlException ex)
            {
                ret = false;
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }

       /// <summary>
       /// 获取当天明细对帐文件的总额信息。
       /// </summary>
       /// <param name="businessDate">银行编码</param>
       /// <param name="startTime">业务日期</param>
       /// <returns>总金额</returns>
        public static decimal GetDetailAccountTotalMoney(string bankCode, string businessDate)
        {
            DataTable dt = null;
            SqlConnection conn = null;
            businessDate = businessDate.Substring(0, 4) + "-" + businessDate.Substring(4, 2) + "-" + businessDate.Substring(6, 2);
            SqlParameter[] paramList = new SqlParameter[] { DbHelper.CreateCommandParameter("@BankCode", SqlDbType.VarChar, bankCode),
                                                            DbHelper.CreateCommandParameter("@BusinessDate",SqlDbType.DateTime,businessDate) };

            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                dt = DbHelper.GetTableByProcedure("NXS_GetDetailAccountTotalMoney", paramList, conn);
            }
            catch (SqlException ex)
            {
                dt = null;
            }
            finally
            {
                conn.Close();
            }

            return Convert.ToDecimal(dt != null ? (dt.Rows[0][0]==DBNull.Value?0:dt.Rows[0][0]) : 0);
        }


        /// <summary>
        /// 清空明细对帐文件信息
        /// </summary>
        /// <returns></returns>
        public static bool Delete(string bankCode)
        {
            bool ret = true;
            SqlConnection conn = null;

            SqlParameter[] paramList = new SqlParameter[] {DbHelper.CreateCommandParameter("@BankCode",SqlDbType.VarChar,bankCode),
                                                            DbHelper.CreateCommandOutParameter("@return",SqlDbType.Int)};
            try
            {
                conn = DbHelper.CreateDbConnection(DbHelper.DbConnectionString);
                conn.Open();
                ret = DbHelper.ExecuteNonQueryByProc("NXS_DeleteCheckAccountDetailTemp", paramList, conn) > 0;
            }
            catch (SqlException ex)
            {
                ret = false;
            }
            finally
            {
                conn.Close();
            }
            return ret;
        }
    }
}
